package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import org.come.bean.ChatBean;
import org.come.entity.Friendtable;
import org.come.entity.TeamRole;
import org.come.until.AnalysisString;
import org.come.until.CutButtonImage;
import org.come.until.MessagrFlagUntil;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;
import org.come.until.Util;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.RoleCaoZuoBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

/**
 * @author 郑天宇
 * @date : 2017年12月14日 下午9:59:58 好友聊天面板
 */
public class FriendChatMessageJpanel extends JPanel {
	// private JLabel Forname,race,grade;//名称，种族，等级
	private JLabel labNoC, //发送不关闭
			labTx,// 放置好友的头像图片
	labTxD;// 放置好友的头像图片
	private static String ID,// 好友ID
			HyName,// 好友名字
			Grade;// 好友等级
	private JTextArea setwords;// 消息发送框
	private Color fontcolor; // 对象颜色
	private RoleCaoZuoBtn btnfasong;// 发送按钮

	private JScrollPane js;
	private JScrollPane jscrollPane;
	final ImageIcon imageIcon = new ImageIcon("img/123_副本.png");
	private Friendtable friendtable;// 选中的好友信息
	public static boolean closeCk = true; //发送不关闭

	public FriendChatMessageJpanel() throws Exception {
		if(!Util.dhjl) {
			Util.dhjl = true;
			ChatBean chatBean = new ChatBean();
			chatBean.setFriendName("大话精灵");
			chatBean.setMessage("#Y#38您要查询什么呢？如果有我解决不了的问题可以找老G哦！#r#G查询#RNPC#G请发送N=加NPC名字，#Y例如：N=玉皇大帝。#r#G查询#R掉落#G请发送D=物品名字，#Y例如：D=补天神石。#r#G查询#R技能#G请发送J=技能名称，#Y例如：J=夺命勾魂。");
			chatBean.setRolename(ImageMixDeal.userimg.getRoleShow().getRolename());
			chatBean.setSendRoleId(ImageMixDeal.userimg.getRoleShow().getRole_id());
			chatBean.setTime(new java.util.Date().getTime());
			ReceiveMessage1(chatBean, chatBean.getFriendName());
		}
			this.setPreferredSize(new Dimension(584, 404));
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);
			// 键盘监听
			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 56);
			offBtn.setBounds(584-32, 6, 25, 25);
			this.add(offBtn);

			// 放置好友的头像图片
			labTx = new JLabel("",SwingConstants.CENTER);
			labTx.setBounds(22, 17, 40, 40);
			this.add(labTx);

		labTxD = new JLabel("",SwingConstants.CENTER);
		labTxD.setBounds(344, 68, 218, 298);
		this.add(labTxD);
			//发送不关闭
			labNoC = new JLabel("",SwingConstants.CENTER);
			labNoC.setBounds(24, 296, 17, 17);
			labNoC.addMouseListener(new MouseListener() {
				@Override
				public void mouseReleased(MouseEvent e) {
					closeCk = !closeCk;
				}
				@Override
				public void mousePressed(MouseEvent e) {}
				@Override
				public void mouseExited(MouseEvent e) {}
				@Override
				public void mouseEntered(MouseEvent e) {}
				@Override
				public void mouseClicked(MouseEvent e) {}
			});
			this.add(labNoC);
			js = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
					ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			js.getVerticalScrollBar().setUI(new SrcollPanelUI());
			js.getViewport().setOpaque(false);
			js.setOpaque(false);
			js.setBorder(BorderFactory.createLineBorder(new Color(31, 7, 8)));
			js.setViewportView(null);
			js.setBounds(19, 85, 312, 208);
			this.add(js);
			// 输入框
			setwords = new JTextArea() {
				Image image = imageIcon.getImage();
				Image grayImage = GrayFilter.createDisabledImage(image);
				{
					setOpaque(false);
				}

				@Override
				public void paint(Graphics g) {
					g.drawImage(grayImage, 0, 0, this);
					super.paint(g);
				}

			};
			setwords.setCaretColor(Color.yellow); // 设置光标颜色
			setwords.setLineWrap(true);
			setwords.setForeground(Color.white);
			setwords.setBackground(UIUtils.Color_BACK);
			setwords.setBorder(BorderFactory.createEmptyBorder());
			setwords.setFont(UIUtils.TEXT_FONT1);
			jscrollPane = new JScrollPane(setwords);
			jscrollPane
					.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
			jscrollPane.getVerticalScrollBar().setUI(new ScrollUI());
			jscrollPane.getViewport().setOpaque(false);
			jscrollPane.setOpaque(false);
			jscrollPane.setBounds(24, 323, 304, 35);
			jscrollPane.setBorder(BorderFactory.createEmptyBorder());
			jscrollPane
					.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			this.add(jscrollPane);	       
			// 发送按钮
			btnfasong = new RoleCaoZuoBtn("skin/8100226/newbutton/B20.png", 1, "发送", 5,UIUtils.COLOR_BLACK);
			btnfasong.setBounds(245, 365, 59, 24);
			this.add(btnfasong);
	}

	public void showFriend(Friendtable friend, RichLabel label) {
		if("非好友".equals(friend.getRace_name())) {
			labTx.setIcon(CutButtonImage.getImage("inkImg/old/icon/ico.png", 40, 40));
			labTxD.setIcon(CutButtonImage.getImage("inkImg/old/icon/ico.png", 218, 298));
			ID = "陌生人";
		} else {
			labTx.setIcon(CutButtonImage.getImage("img/head/s" + friend.getSpecies_id() + ".png", 40, 40));
			labTxD.setIcon(CutButtonImage.getImage("img/head/" + friend.getSpecies_id() + ".png", 218, 298));
			ID = friend.getRole_id().toString();
		}
		HyName = friend.getRolename();
		Grade = AnalysisString.lvl(friend.getGrade().intValue()) + "级";
		js.setViewportView(label);
	}
	public void showFriend(TeamRole teamRole, RichLabel label) {
		labTx.setIcon(CutButtonImage.getImage("img/head/s" + teamRole.getSpeciesId() + ".png", 40, 40));
		labTxD.setIcon(CutButtonImage.getImage("img/head/" + teamRole.getSpeciesId() + ".png", 218, 298));
		HyName = teamRole.getName();
		Grade = AnalysisString.lvl(teamRole.getGrade()) + "级";
		ID = teamRole.getRoleId().toString();
		js.setViewportView(label);
	}
	private ImageIcon icon,icon1;

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
			if (icon == null) {
				icon = new ImageIcon("inkImg/newbackground/B328.png");
            }
			// 信息
			g.drawImage(icon.getImage(), 0, 0, 584, 404, this);
			
			if (HyName!=null) {
				g.setFont(UIUtils.TEXT_FONT1);
				g.setColor(UIUtils.COLOR_NAME16);
				g.drawString(HyName, 73, 36);
				g.drawString(Grade, 73, 57);
//				g.setColor(Color.GREEN);
				g.drawString("ID:"+ID, 78+HyName.length()*16, 37);
				if(closeCk){
					if (icon1 == null) {
                        icon1 = new ImageIcon("inkImg/hongmu/2/showjadesuit.png");
                    }
					g.drawImage(icon1.getImage(), 20, 299, 15, 15, this);
				}	
			}
	}
	public static void ReceiveMessage1(ChatBean bean, String rolename) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("#r#R ");
		buffer.append(bean.getRolename());
		buffer.append(" ");
		buffer.append(MessagrFlagUntil.timeStamp2Date(bean.getTime(), "yyyy-MM-dd HH:mm:ss"));
		buffer.append("#r#W");
		buffer.append(bean.getMessage());
		RichLabel richLabel = MessagrFlagUntil.getRichLabel(rolename);
		richLabel.addText(buffer.toString());
		richLabel.setPreferredSize(richLabel.computeSize(richLabel.getWidth()));
	}
	public JTextArea getSetwords() {
		return setwords;
	}
	public void setSetwords(JTextArea setwords) {
		this.setwords = setwords;
	}
	public RoleCaoZuoBtn getBtnfasong() {
		return btnfasong;
	}
	public void setBtnfasong(RoleCaoZuoBtn btnfasong) {
		this.btnfasong = btnfasong;
	}
	public ImageIcon getImageIcon() {
		return imageIcon;
	}
	public Color getFontcolor() {
		return fontcolor;
	}
	public void setFontcolor(Color fontcolor) {
		this.fontcolor = fontcolor;
	}
	public Friendtable getFriendtable() {
		return friendtable;
	}
	public void setFriendtable(Friendtable friendtable) {
		this.friendtable = friendtable;
	}
	public JScrollPane getJs() {
		return js;
	}
	public void setJs(JScrollPane js) {
		this.js = js;
	}
	public static String getID() {
		return ID;
	}
	public static void setID(String iD) {
		ID = iD;
	}
	public static String getHyName() {
		return HyName;
	}
	public static void setHyName(String hyName) {
		HyName = hyName;
	}
	public static String getGrade() {
		return Grade;
	}
	public static void setGrade(String grade) {
		Grade = grade;
	}
	public JScrollPane getJscrollPane() {
		return jscrollPane;
	}
	public void setJscrollPane(JScrollPane jscrollPane) {
		this.jscrollPane = jscrollPane;
	}
}
